
/**
 * 时间工具类
 */
export class TimeUtil {

    protected static _timeMap = new Map<any, number>();
    protected static _getTime = (() => {
        const performance = (globalThis || window).performance;
        if (performance) {
            return performance.now.bind(performance);
        }
        return Date.now.bind(Date);
    })();

    /**
     * 开始计时
     * @param key 计时key
     * @param cover 是否覆盖上次
     */
    static timeStart(key: any, cover = true) {
        if (cover || !this._timeMap.has(key)) {
            this._timeMap.set(key, this._getTime());
        }
    }

    /**
     * 结束计时
     * @param key 计时key
     * @returns 计时时长(ms)
     */
    static timeEnd(key: any) {
        if (this._timeMap.has(key)) {
            return this._getTime() - this._timeMap.get(key);
        }
        return 0;
    }

}